package com.ruoyi.house_sale.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.house_sale.domain.PaidScheme;
import com.ruoyi.house_sale.mapper.PaidSchemeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.house_sale.mapper.SchemeflowMapper;
import com.ruoyi.house_sale.domain.Schemeflow;
import com.ruoyi.house_sale.service.ISchemeflowService;

import javax.annotation.Resource;
import javax.validation.Validator;

/**
 * 方案流程Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-28
 */
@Service
public class SchemeflowServiceImpl implements ISchemeflowService 
{
    private static final Logger log = LoggerFactory.getLogger(SchemeflowServiceImpl.class);

    @Resource
    private SchemeflowMapper schemeflowMapper;

    @Resource
    private PaidSchemeMapper paidSchemeMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询方案流程
     * 
     * @param flowid 方案流程主键
     * @return 方案流程
     */
    @Override
    public Schemeflow selectSchemeflowByFlowid(Long flowid)
    {
        return schemeflowMapper.selectSchemeflowByFlowid(flowid);
    }

    /**
     * 查询方案流程列表
     * 
     * @param schemeflow 方案流程
     * @return 方案流程
     */
    @Override
    public List<Schemeflow> selectSchemeflowList(Schemeflow schemeflow)
    {
        return schemeflowMapper.selectSchemeflowList(schemeflow);
    }

    /**
     * 新增方案流程
     * 
     * @param schemeflow 方案流程
     * @return 结果
     */
    @Override
    public int insertSchemeflow(Schemeflow schemeflow)
    {
        return schemeflowMapper.insertSchemeflow(schemeflow);
    }

    /**
     * 修改方案流程
     * 
     * @param schemeflow 方案流程
     * @return 结果
     */
    @Override
    public int updateSchemeflow(Schemeflow schemeflow)
    {
        return schemeflowMapper.updateSchemeflow(schemeflow);
    }

    /**
     * 批量删除方案流程
     * 
     * @param flowids 需要删除的方案流程主键
     * @return 结果
     */
    @Override
    public int deleteSchemeflowByFlowids(Long[] flowids)
    {
        return schemeflowMapper.deleteSchemeflowByFlowids(flowids);
    }

    /**
     * 删除方案流程信息
     * 
     * @param flowid 方案流程主键
     * @return 结果
     */
    @Override
    public int deleteSchemeflowByFlowid(Long flowid)
    {
        return schemeflowMapper.deleteSchemeflowByFlowid(flowid);
    }

    /**
     * 导入方案流程数据
     *
     * @param schemeflowList 方案流程数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importFlow(List<Schemeflow> schemeflowList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(schemeflowList) || schemeflowList.size() == 0)
        {
            throw new ServiceException("导入方案流程数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Schemeflow flow : schemeflowList)
        {
            try
            {
                // 验证是否存在这个付款方案
                PaidScheme ps = paidSchemeMapper.selectPaidSchemeByPsid(flow.getPsid());
                if(!StringUtils.isNull(ps))
                {
                    // 验证是否存在这个方案流程
                    Schemeflow sf = new Schemeflow();
                    sf.setPsid(flow.getPsid());
                    sf.setFlowname(flow.getFlowname());
                    List<Schemeflow> f = schemeflowMapper.selectSchemeflowList(sf);
                    if (f.size() < 1)
                    {
                        BeanValidators.validateWithException(validator, flow);
                        this.insertSchemeflow(flow);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、付款方案 " + flow.getPsid() + " 流程 " + flow.getFlowname() + " 导入成功");
                    }
                    else if (isUpdateSupport)
                    {
                        flow.setFlowid(f.get(0).getFlowid());
                        BeanValidators.validateWithException(validator, flow);
                        this.updateSchemeflow(flow);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、付款方案 " + flow.getPsid() + " 流程 " + flow.getFlowname() + " 更新成功");
                    }
                    else
                    {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、付款方案 " + flow.getPsid() + " 流程 " + flow.getFlowname() + " 已存在");
                    }
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、付款方案 " + flow.getPsid() + " 不存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、付款方案 " + flow.getPsid() + " 流程 " + flow.getFlowname() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
